/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Action.AccountSessionBeanLocal;
import Entity.Account;
import Entity.Customer;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cyrax
 */
@ManagedBean
@SessionScoped
public class AccountBean {

    @EJB
    private AccountSessionBeanLocal accountSessionBean;
    /** Creates a new instance of AccountBean */
    private Account ac;
    private List<Account> listAcc;
    private Customer cu;
    private String email;

    public AccountBean() {
        ac = new Account();
    }

    /**
     * @return the ac
     */
    public Account getAc() {
        return ac;
    }

    /**
     * @param ac the ac to set
     */
    public void setAc(Account ac) {
        this.ac = ac;
    }

    public void registerAccount() {
        try {


        ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();
        FacesContext face = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) face.getExternalContext().getSession(true);
        cu=new Customer();
        cu.setEmail((String) session.getAttribute("userEmail"));
        ac.setCustomer(cu);
        accountSessionBean.insertAccount(ac);
        System.out.println("Xem email"+ ac.getCustomer().getEmail());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Account> getListAccount() {
        try {
            listAcc = accountSessionBean.listAllAccount();
            System.out.println("hello:" + listAcc);

        } catch (Exception e) {
            System.out.println("Eror list book : " + e.getMessage());
        }
        return listAcc;
    }

    public void deleteAccount(Account ac) {
        try {
            accountSessionBean.deleteAccount(ac);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private String mess;

    public void checkAccountId() {
        if (accountSessionBean.checkAccountId(ac.getAccountId())) {

            mess = "This User Name is already existed";
        } else {
            mess = "";

        }


    }

    public void updateAccount() {
        try {

            String name = ac.getAccountId();
//            cu=new Customer();
//            cu.setEmail(email);
//            ac.setCustomer(cu);
            System.out.println("Xem email" + ac.getCustomer().getEmail());
            accountSessionBean.updateAccount(name, ac.getAccountName(), ac.getAmount(), ac.getCustomer().getEmail());
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    /**
     * @return the mess
     */
    public String getMess() {
        return mess;
    }

    /**
     * @param mess the mess to set
     */
    public void setMess(String mess) {
        this.mess = mess;
    }

    /**
     * @return the cu
     */
    public Customer getCu() {
        return cu;
    }

    /**
     * @param cu the cu to set
     */
    public void setCu(Customer cu) {
        this.cu = cu;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
}
